package me.arraylist.demo.Student;

import java.util.ArrayList;
import java.util.Scanner;


public class StudentTest {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("----------欢迎来到学生管理系统----------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的选择");
            int choose = sc.nextInt();
            switch (choose) {
                case 1 -> addStudent(list);
                case 2 -> deleteStudent(list);
                case 3 -> updateStudent(list);
                case 4 -> queryStudent(list);
                case 5 -> {
                    System.out.println("谢谢使用");
                    break loop;
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    private static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("暂无学生信息");
            return;
        } else {
            System.out.println("id\tname\tage\taddress");
            for (int i = 0; i < list.size(); i++) {
                Student st = list.get(i);
                System.out.println(st.getId() + "\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getAddress());
            }
        }

    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生id");
        String id = sc.next();
        boolean falg = contains(list, id);
        if (falg) {
            int index = getIndex(list, id);
            Student st = list.get(index);
            System.out.println("请输入要修改的学生姓名");
            String name = sc.next();
            st.setName(name);
            System.out.println("请输入要修改的学生年龄");
            int age = sc.nextInt();
            st.setAge(age);
            System.out.println("请输入要修改的学生地址");
            String address = sc.next();
            st.setAddress(address);
            System.out.println("修改成功");
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        boolean falg = contains(list, id);
        if (falg) {
            int index = getIndex(list, id);
            list.remove(index);
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
            return;
        }

    }

    private static void addStudent(ArrayList<Student> list) {
        Student st = new Student();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生id");
            String id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("该id已存在");
            } else {
                st.setId(id);
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        st.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        st.setAge(age);
        System.out.println("请输入学生地址");
        String address = sc.next();
        st.setAddress(address);
        list.add(st);
        System.out.println("添加成功");
    }

    private static boolean contains(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            if (st.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    private static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            if (st.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
